package com.liuzhongni.wahahanews.fragment.newcolumnfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.adapter.RecyclerViewAdapterOfPicture;
import com.liuzhongni.wahahanews.singleton.StringRequestUtf;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;

import java.lang.reflect.Type;
import java.util.ArrayList;

import com.liuzhongni.wahahanews.pictureclass.PictureDatails;

/**
 * Created by admin on 2016/1/8.
 */
public class PictureFragment extends Fragment  {
    private RecyclerView recyclerView;
    private RecyclerViewAdapterOfPicture adapter;
    private ArrayList<PictureDatails> beans;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_column_picture,null);
        recyclerView = (RecyclerView) view.findViewById(R.id.new_column_picture_rv);
        Log.d("PictureFragment", "哇哈哈");
        json();

        return view;
    }

    private void json() {
        String url = "http://c.m.163.com/photo/api/list/0096/4GJ60096.json";
        StringRequestUtf request = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("PictureFragment", response);
                Gson gson = new Gson();
                Type type = new TypeToken<ArrayList<PictureDatails>>() {
                }.getType();
                Log.d("PictureFragment", "哈哈哈");
                beans = gson.fromJson(response, type);
                init();
            }
        },null);
        VolleySingleton.getOurInstance().getQueue().add(request);
    }

    private void init() {
        adapter = new RecyclerViewAdapterOfPicture(getActivity(),beans);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
    }

}
